package com.scau402.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ClientCodeUtils;
import com.scau402.common.utils.code.SupplierCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.domain.WmsBasicClient;
import com.scau402.system.service.ISysTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.scau402.common.annotation.Log;
import com.scau402.common.core.controller.BaseController;
import com.scau402.common.core.domain.AjaxResult;
import com.scau402.common.enums.BusinessType;
import com.scau402.system.domain.WmsBasicSupplier;
import com.scau402.system.service.IWmsBasicSupplierService;
import com.scau402.common.utils.poi.ExcelUtil;
import com.scau402.common.core.page.TableDataInfo;

/**
 * 供应商管理Controller
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/system/supplier")
@Slf4j
public class WmsBasicSupplierController extends BaseController {
    @Autowired
    private IWmsBasicSupplierService wmsBasicSupplierService;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsBasicSupplier wmsBasicSupplier) {
        log.info("查询供应商管理列表, 参数: {}", wmsBasicSupplier);
        startPage();
        List<WmsBasicSupplier> list = wmsBasicSupplierService.selectWmsBasicSupplierList(wmsBasicSupplier);
        log.debug("查询供应商管理列表, 结果: {}", list);
        return getDataTable(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsBasicSupplier wmsBasicSupplier) {
        log.info("导出供应商管理列表, 参数: {}", wmsBasicSupplier);
        List<WmsBasicSupplier> list = wmsBasicSupplierService.selectWmsBasicSupplierList(wmsBasicSupplier);
        log.debug("导出供应商管理列表, 结果: {}", list);
        ExcelUtil<WmsBasicSupplier> util = new ExcelUtil<WmsBasicSupplier>(WmsBasicSupplier.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        log.info("获取供应商管理详细信息, 参数: {}", id);
        return success(wmsBasicSupplierService.selectWmsBasicSupplierById(id));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WmsBasicSupplier wmsBasicSupplier) {
        log.info("新增供应商管理, 参数: {}", wmsBasicSupplier);
        wmsBasicSupplier.setTenantId(getLoginUser().getTenantId());
        if (!wmsBasicSupplierService.checkNameUnique(wmsBasicSupplier)) {
            return error("供应商名称已存在");
        } else if (!wmsBasicSupplierService.checkCodeUnique(wmsBasicSupplier)) {
            return error("供应商编码已存在");
        }
        return toAjax(wmsBasicSupplierService.insertWmsBasicSupplier(wmsBasicSupplier));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WmsBasicSupplier wmsBasicSupplier) {
        log.info("修改供应商管理, 参数: {}", wmsBasicSupplier);
        return toAjax(wmsBasicSupplierService.updateWmsBasicSupplier(wmsBasicSupplier));
    }

    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        log.info("删除供应商管理, 参数: {}", ids);
        return toAjax(wmsBasicSupplierService.deleteWmsBasicSupplierByIds(ids));
    }

    /**
     * 获取供应商编码
     *
     * @return
     * @throws InterruptedException
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:add')")
    @GetMapping("/getSupplierCode")
    public String getSupplierCode() throws InterruptedException {
        // 返回租户编码
        return wmsBasicSupplierService.getNewCode(SecurityUtils.getLoginUser().getUser());
    }

    /**
     * 启用、停用供应商
     * @param wmsBasicSupplierList
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:supplier:status')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping("/startOrStop")
    public AjaxResult startOrStop(@Validated @RequestBody List<WmsBasicSupplier> wmsBasicSupplierList){
        log.info("启用停用供应商管理, 参数: {}", wmsBasicSupplierList);
        return toAjax(wmsBasicSupplierService.startOrStop(wmsBasicSupplierList));
    }
}
